<template>
    <Button type="primary" :disabled="sending" @click="onClick">{{value}}</Button>
</template>

<script>


    export default {
        name: "VfcButton",
        data() {
            return {
                sending: false,
                value: '获取验证码'
            }
        },
        props: {
          tel: ''
        },
        methods: {
            onClick() {
                if (!/^\d{11}$/.test(this.tel)) {
                    this.n9.error("手机号有误");
                    return false;
                }
                this.sending = true;
                    ajax.get('/p/telVFCode/send', { tel: this.tel }, (res) => {
                    // ajax.get('http://www.9ip.com:8088/p/telVFCode/send', { tel: this.tel }, (res) => {
                        this.n9.success("验证码已发送至" + this.tel);
                        var oldText = this.value;
                        var timeout = 60;
                        this.value = timeout + "s重新发送";
                        //var me = this
                        var it = setInterval(() => {
                            if (--timeout >= 0) {
                                this.value = timeout + "s重新发送";
                            } else {
                                clearInterval(it);
                                this.value = oldText;
                                this.sending = false;
                            }
                        }, 1000);
                    }, (res) => {
                        this.sending = false;
                    });
            }
        }
    }
</script>

<style scoped>

</style>